import requests
from bs4 import BeautifulSoup

def search_duckduckgo(query):
    base_url = "https://duckduckgo.com/html/"
    params = {
        "q": query
    }
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x86) AppleWebKit/5.0 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    try:
        response = requests.get(base_url, params=params, headers=headers)
        response.raise_for_status()
        soup = BeautifulSoup(response.text, 'html.parser')
        # 这里以获取搜索结果的标题和链接为例，提取相关信息
        search_results = []
        for result in soup.select('.result__a'):
            title = result.text
            link = result.get('href')
            search_results.append({"title": title, "link": link})
        return search_results
    except requests.RequestException as e:
        print(f"请求出错: {e}")
        return []
    except Exception as e:
        print(f"发生其他错误: {e}")
        return []

# 示例使用
query = "人工智能发展现状"
results = search_duckduckgo(query)
for res in results:
    print(res["title"])
    print(res["link"])
    print("-" * 50)